package com.example.demo.controller;

import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import com.example.demo.common.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.*;

@Api(tags = "用户管理")
@Log4j2
@Secured({"ROLE_USER"})
@RequestMapping("user")
@RestController
public class UserController {
    @Autowired
    UserService userService;

    @ApiOperation(value = "查询列表")
    @GetMapping("list")
    public R list(
            @RequestParam(defaultValue = "1") int current,
            @RequestParam(defaultValue = "10") int size
    ) {
        return R.ok(userService.list(current, size));
    }
    @GetMapping("detail")
    public R detail(long id){
        //userService.query().eq("id", id).one();
        //userService.lambdaQuery().eq(User::getId, id).one();
        return R.ok(userService.getById(id));
    }

    @PostMapping("save")
    public R save(User user){
        return R.okFail(userService.saveOrUpdate(user));
    }
    @GetMapping("remove/{id}")
    public R remove(@PathVariable("id") long id){
        return R.okFail(userService.removeById(id));
    }
}
